import 'package:dog_read/pages/book/sites/biquge.dart';
import 'package:dog_read/provider/providers.dart';
import 'package:flutter/material.dart';
import 'package:dog_read/components/dotted_separator.dart';
import 'package:provider/provider.dart';

class ChapterList extends StatefulWidget {

  @override
  _ChapterListState createState() => _ChapterListState();
}

class _ChapterListState extends State<ChapterList> {
  var controller = new ScrollController();

  List chapterList;
  String bookTitle;

  @override
  void initState() {
    super.initState();
    this.chapterList = Provider.of<ChapterInfosProvider>(context, listen: false).chapterList;
    this.bookTitle = Provider.of<ChapterInfosProvider>(context, listen: false).bookTitile;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('章节目录'),
        elevation: 0.0,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          onPressed: (){
            // Navigator.pop(context, true);
             Navigator.pop(context, () {
                setState(() {});});
          },
        ),
      ),
      body: ListView.builder(
          controller: this.controller,
          itemCount: chapterList.length,
          itemBuilder: (BuildContext context, int index) {
            return Column(
              children: <Widget>[
                ListTile(
                    onTap: (){
                      Provider.of<ChapterInfosProvider>(context, listen: false).setChapterIndex(index);
                      Navigator.pushNamed(context, "/chapter/content");
                    },
                    title: Text('${chapterList[index]["title"]}')),
                MySeparator()
              ],
            );
          }),
      floatingActionButton: FloatingActionButton(
        child: Text("⬆️"),
        onPressed: () {
          int durationTime;
          if (this.controller.offset > 5000) {
            durationTime = 500;
          } else {
            durationTime = 200;
          }
          this.controller.animateTo(
                0,
                duration: Duration(milliseconds: durationTime),
                curve: Curves.easeInOut,
              );
        },
      ),
    );
  }
}
